package com.msb.test14;

import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;

public class Test {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int choice;

        while (true){
            choice = showMenu(scanner);
            if (choice == 4){break;}
            switch (choice) {
                case 1:{
                    try {
                        showBook();
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    } catch (ClassNotFoundException e) {
                        throw new RuntimeException(e);
                    }
                    break;
                }
                case 2:{

                    try {
                        addBook(scanner);
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    } catch (ClassNotFoundException e) {
                        throw new RuntimeException(e);
                    }

                    break;
                }
                case 3:{
//                    removeBook();
                    break;
                }
                case 4:{
                    break;
                }
            }




        }

    }
    public static int showMenu(Scanner scanner){
        System.out.println("欢迎来到【老马书城】");
        System.out.println("1.展示书籍");
        System.out.println("2.上新书籍");
        System.out.println("3.下架书籍");
        System.out.println("4.退出应用");
        System.out.println("请选择您的操作：");
        int number = scanner.nextInt();
        scanner.nextLine();
        return number;

    }

    public static void showBook() throws IOException, ClassNotFoundException {
        String currentDir = System.getProperty("user.dir");

        //文件对象
        File file = new File(currentDir+"/TestFirstModule/src/com/msb/test14/books.txt");
        if (file.exists()==true) {
            FileInputStream fis = new FileInputStream(file);
            ObjectInputStream ois = new ObjectInputStream(fis);
            ArrayList books = (ArrayList) (ois.readObject());
            ois.close();
            System.out.println(books);
        }else {
            System.out.println("暂没书籍，请先添加书籍");
        }


    }

    public static void addBook(Scanner scanner) throws IOException, ClassNotFoundException {

        System.out.println("请输入书籍编号");
        int bNo = scanner.nextInt();
        scanner.nextLine();
        System.out.println("请输入书籍名称");
        String bName = scanner.nextLine();
        System.out.println("请输入书籍作者");
        String bAuthor = scanner.nextLine();
        Book book = new Book(bNo,bName,bAuthor);
        ArrayList books = new ArrayList();
        String currentDir = System.getProperty("user.dir");
//        System.out.println("当前工作目录: " + currentDir);
        //文件对象
        File file = new File(currentDir+"/TestFirstModule/src/com/msb/test14/books.txt");
        if (file.exists()) {
            FileInputStream fis = new FileInputStream(file);
            ObjectInputStream ois = new ObjectInputStream(fis);
            books = (ArrayList) (ois.readObject());


            if (books.isEmpty()) {
                books = new ArrayList();
            }
        }
        books.add(book);
        FileOutputStream fos = new FileOutputStream(file);
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        oos.writeObject(books);
        oos.close();
        System.out.println(books);

    }

    public static void removeBook(ArrayList books,Scanner scanner ) {
        System.out.println("请输入书籍名称");
        String book = scanner.nextLine();
        for (int i = 0; i < books.size(); i++) {
            if (books.get(i).equals(book)) {
                books.remove(book);
            }
        }

        System.out.println(books);

    }
}
